package com.template;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;

import com.template.utils.TStringUtils;

public class TService {

	public static void createXMLFile(String tableName, String packageName,List<TableBean>columnList,List<TableBean>priList, List<TableBean>noPriList, String folderName, String path) {
		String className = TStringUtils.tableName2ClassName(tableName);
	    String attrName = TStringUtils.columnName2Attr(tableName);
	    String str = "";
	    str+= "package "+packageName+".service.impl;\r\n";
	    str+="import java.io.ByteArrayInputStream;\r\n";
	    str+="import java.io.File;\r\n";
	    str+="import java.io.InputStream;\r\n";
	    str+="import java.util.HashMap;\r\n";
	    str+="import java.util.List;\r\n\r\n";
        str+="import org.apache.log4j.Logger;\r\n";
        str+="import org.springframework.beans.factory.annotation.Autowired;\r\n";
	    str+="import org.springframework.beans.factory.annotation.Value;\r\n";
	    str+="import org.springframework.stereotype.Service;\r\n\r\n";
	    str+="import com.alibaba.druid.util.Base64;\r\n";
	    str+="import "+packageName+".dao."+className+"Dao;\r\n";
	    str+="import "+packageName+".entity."+className+";\r\n";
	    str+="import "+packageName+".entity.ResponseModel;\r\n";
	    str+="import "+packageName+".entity.ResultEnum;\r\n";
	    str+="import "+packageName+".service.I"+className+"Service;\r\n";
	    str+="import "+packageName+".util.ParameterMap;\r\n";
	    str+="import "+packageName+".util.Tools;\r\n\r\n";
        str+="@Service\r\n";
        str+="public class "+className+"Service implements I"+className+"Service {\r\n\r\n";
	    str+="    private Logger log = Logger.getLogger(this.getClass());\r\n";
	    str+="    @Autowired\r\n";
	    str+="    private "+className+"Dao "+attrName+"dao;\r\n\r\n\r\n";
        str+="    @Override\r\n";
        str+="    public List<ParameterMap> get"+className+"List() {\r\n";
        str+="        // TODO Auto-generated method stub\r\n";
        str+="      return "+attrName+"dao.list();\r\n";
        str+="   }\r\n\r\n";
	    str+="    @Override\r\n";
        str+="    public HashMap<String, Object> add(ParameterMap pm) {\r\n";
	    str+="        // TODO Auto-generated method stub\r\n";
	    str+="      try {\r\n";
	    str+="         "+attrName+"dao.add"+className+"(pm);\r\n";
	    str+="         }catch(Exception e) {\r\n";
	    str+="          log.error(\"error:\"+e.getMessage(), e);\r\n";
	    str+="          return ResponseModel.getModel(ResultEnum.ERROR, null);\r\n";
	    str+="         }\r\n";
	    str+="   return ResponseModel.getModel(ResultEnum.SUCCESS, null);\r\n";
	    str+="  }\r\n\r\n\r\n";
        str+="     @Override\r\n";
        str+="    public HashMap<String, Object> edit(ParameterMap pm) {\r\n";
        str+="        // TODO Auto-generated method stub\r\n";
        str+="        try {\r\n";
        str+="             "+attrName+"dao.edit"+className+"(pm);\r\n";
        str+="           }catch(Exception e) {\r\n";
        str+="            log.error(\"error:\"+e.getMessage(), e);\r\n";
        str+="            return ResponseModel.getModel(ResultEnum.ERROR, null);\r\n";
        str+="           }\r\n";
        str+="         return ResponseModel.getModel(ResultEnum.SUCCESS, null);\r\n";
        str+="  }\r\n\r\n";
	    str+="       @Override\r\n";
	    String type = "String";
		if ("int".equals(priList.get(0).getDataType())) {
			type = "long";
		} else if ("date".equals(priList.get(0).getDataType())
				|| "datetime".equals(priList.get(0).getDataType())
				|| "timestamp".equals(priList.get(0).getDataType())) {
			type = "Date";
		}
	    str+="       public HashMap<String, Object> del("+type+" "+priList.get(0).getColumnName()+") {\r\n";
        str+="           // TODO Auto-generated method stub\r\n";
        str+="        try\r\n";
        str+="        {\r\n";
	    str+="        "+attrName+"dao.del"+className+"("+priList.get(0).getColumnName()+");\r\n";
	    str+="        }catch (Exception e) {\r\n";
	    str+="          // TODO: handle exception\r\n";
	    str+="          e.printStackTrace();\r\n";
	    str+="          return ResponseModel.getModel(ResultEnum.ERROR, null);\r\n";
	    str+="        }\r\n";
	    str+="       return ResponseModel.getModel(ResultEnum.SUCCESS, null);\r\n";
	    str+="      }\r\n\r\n\r\n";
	    str+="   @Override\r\n";
	    str+="   public "+className+" get"+className+"Info(ParameterMap pm) {\r\n";
	    str+="       // TODO Auto-generated method stub\r\n";
	    str+="      return null;\r\n";
	    str+="   }\r\n\r\n";
	    str+="}\r\n";
	    
	    
		try {
			String allPath = path+"\\"+className+"Service.java";
			OutputStreamWriter output = new OutputStreamWriter(new FileOutputStream(allPath),"UTF-8");
			output.write(str);
			output.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
